package com.kalvin.kvf.modules.hxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.sys.entity.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.hxy.entity.Cookies;
import com.kalvin.kvf.modules.hxy.service.CookiesService;

import javax.security.auth.Subject;
import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * cookies 前端控制器
 * </p>
 * @since 2023-11-23 16:58:16
 */
@RestController
@RequestMapping("hxy/cookies")
public class CookiesController extends BaseController {

    @Autowired
    private CookiesService cookiesService;

    @RequiresPermissions("hxy:cookies:index")
    @GetMapping("dyindex")
    public ModelAndView dyindex() {
        return new ModelAndView("hxy/dy_cookies");
    }

    @RequiresPermissions("hxy:cookies:index")
    @GetMapping("ksindex")
    public ModelAndView ksindex() {
        return new ModelAndView("hxy/ks_cookies");
    }

    @RequiresPermissions("hxy:cookies:index")
    @GetMapping("xhsindex")
    public ModelAndView xhsindex() {
        return new ModelAndView("hxy/xhs_cookies");
    }


    @GetMapping(value = "dyedit")
    public ModelAndView dyedit(Long id) {
        ModelAndView mv = new ModelAndView("hxy/dy_cookies_edit");
        Cookies cookies;
        if (id == null) {
            cookies = new Cookies();
        } else {
            cookies = cookiesService.getById(id);
        }
        mv.addObject("editInfo", cookies);
        return mv;
    }


    @GetMapping(value = "ksedit")
    public ModelAndView ksedit(Long id) {
        ModelAndView mv = new ModelAndView("hxy/ks_cookies_edit");
        Cookies cookies;
        if (id == null) {
            cookies = new Cookies();
        } else {
            cookies = cookiesService.getById(id);
        }
        mv.addObject("editInfo", cookies);
        return mv;
    }


    @GetMapping(value = "xhsedit")
    public ModelAndView xhsedit(Long id) {
        ModelAndView mv = new ModelAndView("hxy/xhs_cookies_edit");
        Cookies cookies;
        if (id == null) {
            cookies = new Cookies();
        } else {
            cookies = cookiesService.getById(id);
        }
        mv.addObject("editInfo", cookies);
        return mv;
    }




    @RequiresPermissions("hxy:cookies:add")
    @PostMapping(value = "add")
    public R add(Cookies cookies) {

        //验证快手cookie

        User user = ShiroKit.getUser();
        cookies.setPhone(user.getPhone());
        cookies.setCreateUser(user.getUsername());
        cookies.setCreateTime(LocalDateTime.now());
        cookies.setLastLoginTime(LocalDateTime.now());
        cookiesService.save(cookies);
        return R.ok();
    }


    @RequiresPermissions("hxy:cookies:edit")
    @PostMapping(value = "edit")
    public R edit(Cookies cookies) {
        cookiesService.updateById(cookies);
        return R.ok();
    }





    @GetMapping(value = "list/data")
    public R listData(Cookies cookies) {
        User user = ShiroKit.getUser();
        cookies.setPhone(user.getPhone());
        Page<Cookies> page = cookiesService.listCookiesPage(cookies);
        return R.ok(page);
    }



    @RequiresPermissions("hxy:cookies:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        cookiesService.removeByIds(ids);
        return R.ok();
    }






    @RequiresPermissions("hxy:cookies:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        cookiesService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(cookiesService.getById(id));
    }

}

